import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import os

# 检查字体文件路径
font_path = r"c:\windows\fonts\msyh.ttc"

if not os.path.isfile(font_path):
    raise FileNotFoundError(f"Font file not found at {font_path}. Please check the path.")

# 读取CSV文件
data = pd.read_csv("cleaned_data.csv")

# 计算casual列中count的总和
casual_count_sum = data['casual'].sum()

# 计算registered列中count的总和
registered_count_sum = data['registered'].sum()

# 计算总租车人数
total_count = casual_count_sum + registered_count_sum

# 计算casual列中count总和的占比
casual_percentage = (casual_count_sum / total_count) * 100

# 计算registered列中count总和的占比
registered_percentage = (registered_count_sum / total_count) * 100

# 指定中文字体
font = FontProperties(fname=font_path, size=12)

# 创建饼图
labels = ['非注册用户', '注册用户']
sizes = [casual_percentage, registered_percentage]
colors = ['lightcoral', 'lightskyblue']
explode = (0.1, 0)  # 突出显示非注册用户部分

# 绘制饼图
plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', startangle=140, textprops={'fontproperties': font})
plt.axis('equal')  # 保持饼图圆形
plt.title('注册用户和非注册用户租车人数占比', fontproperties=font)
plt.show()
